//
//  CQHorizontalViewController.m
//  Pods
//
//  Created by 陈长青 on 2017/4/5.
//
//

#import "CQHorizontalViewController.h"

#define screen_width [UIScreen mainScreen].bounds.size.width
#define screen_height [UIScreen mainScreen].bounds.size.height

@interface CQHorizontalViewController ()

@end

@implementation CQHorizontalViewController

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 图片浏览视图
    
    CQHorizontalView *horizontalView = [CQHorizontalView new];
    [self.view addSubview:horizontalView];
    _horizontalView = horizontalView;
    
    _horizontalView.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *views = NSDictionaryOfVariableBindings(_horizontalView);
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_horizontalView]-0-|" options:nil metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_horizontalView]-0-|" options:nil metrics:nil views:views]];
    
    // 刷新
    
    _horizontalView.dataSource = self;
    
    // 监听设备方向
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
}

- (BOOL)shouldAutorotate{
    
    return NO;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"%s",__FUNCTION__);
}

#pragma mark - 私有方法

// 处理旋转过程中需要的操作

- (void)orientationChanged:(NSNotification *)note  {
    
    UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
    switch (o) {
        case UIDeviceOrientationPortrait:            // Device oriented vertically, home button on the bottom
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
            [self fit:CGAffineTransformIdentity];
            break;
        case UIDeviceOrientationPortraitUpsideDown:  // Device oriented vertically, home button on the top
            
            break;
        case UIDeviceOrientationLandscapeLeft:      // Device oriented horizontally, home button on the right
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
            [self fit:CGAffineTransformMakeRotation(M_PI*0.5)];
            break;
        case UIDeviceOrientationLandscapeRight:      // Device oriented horizontally, home button on the left
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
            [self fit:CGAffineTransformMakeRotation(-M_PI*0.5)];
            
            break;
        default:
            
            break;
    }
}


- (void)fit:(CGAffineTransform) transform {
    
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    
    [UIView animateWithDuration:duration animations:^{
        self.view.transform = transform;
        self.view.bounds = CGRectMake(0, 0, screen_width, screen_height);
    } completion:^(BOOL finished) {
        
    }];
}


#pragma mark - CQHorizontalViewDataSource

- (NSInteger)numberOfCellsInHorizontalView:(CQHorizontalView *)horizontalView {
    
    return 0;
}

- (UIView<CQHorizontalViewCellProtocol> *)horizontalView:(CQHorizontalView *)horizontalView cellForIndex:(NSInteger)index {
    
    UIView<CQHorizontalViewCellProtocol> *cell;
    
    return cell;
}

- (CGFloat)widthForHorizontalView:(CQHorizontalView *)horizontalView {
    
    return [UIScreen mainScreen].bounds.size.width;
}

- (CGFloat)heightForHorizontalView:(CQHorizontalView *)horizontalView {
    
    return [UIScreen mainScreen].bounds.size.height;
}

- (UIView<CQToolBarProtocol> *)horizontalView:(CQHorizontalView *)horizontalView toolBarForIndex:(NSInteger)index {
    
    return nil;
}

- (UIView<CQNavigationBarProtocol> *)horizontalView:(CQHorizontalView *)horizontalView navigationBarForIndex:(NSInteger)index {
    
    return nil;
}

@end
